package logic

import (
	"strconv"
	"web-app/dao/mysql"
	"web-app/dao/redis"
	"web-app/models"
	"web-app/pkg/snowflake"

	"go.uber.org/zap"
)

func CreatePost(p *models.Post) (err error) {
	//生成id
	p.ID = snowflake.GenID()
	err = mysql.CreatePost(p)
	if err != nil {
		return
	}
	err = redis.CreatePost(p.ID,p.CommunityID)
	return

}

func GetPostDetail(id int64) (data *models.ApiPostDetail, err error) {
	//查询并组合我们接口想用的数据

	post, err := mysql.GetPostDetail(id)
	if err != nil {
		zap.L().Error("mysql.GetPostDetail failed ", zap.Error(err))
		return
	}

	username, err := mysql.GetUserByID(post.AuthorID)
	if err != nil {
		zap.L().Error("mysql.GetUserByID failed ", zap.Error(err))
		return
	}
	community, err := mysql.GetCommunityDetailByID(post.CommunityID)
	if err != nil {
		zap.L().Error("mysql.GetCommunityDetailByID ", zap.Error(err))
		return
	}
	data = &models.ApiPostDetail{
		Post:            post,
		AuthorName:      username,
		CommunityDetail: community,
	}
	return

}

// func GetPostList(page int64, size int64) (data []*models.ApiPostDetail, err error) {
// 	posts, err := mysql.GetPostList(page, size)
// 	if err != nil {
// 		zap.L().Error("mysql.GetPostDetail failed ", zap.Error(err))
// 		return
// 	}
// 	data = make([]*models.ApiPostDetail, 0, 2)
// 	for _, post := range posts {
// 		username, err := mysql.GetUserByID(post.AuthorID)
// 		if err != nil {
// 			zap.L().Error("mysql.GetUserByID failed ", zap.Error(err))
// 			continue
// 		}
// 		community, err := mysql.GetCommunityDetailByID(post.CommunityID)
// 		if err != nil {
// 			zap.L().Error("mysql.GetCommunityDetailByID ", zap.Error(err))
// 			continue
// 		}
// 		postDetail := &models.ApiPostDetail{
// 			Post:            post,
// 			AuthorName:      username,
// 			CommunityDetail: community,
// 		}
// 		data = append(data, postDetail)
// 	}
// 	return
// }

func GetPostList2(p *models.ParamPostList) (data []*models.ApiPostDetail, err error) {
	ids, err := redis.GetPostIDsInOrder(p)
	if err != nil {
		return
	}

	if len(ids) == 0 {
		zap.L().Warn("redis.GetPostIDsInOrder return 0 data")
		return
	}
	zap.L().Debug("ids", zap.Any("ids", ids))

	voteData, err := redis.GetPostvotedData(ids)
	zap.L().Debug("voteData", zap.Any("voteData", voteData))
	if err != nil {
		return
	}
	//根据id去mysql数据库查询帖子列表
	posts, err := mysql.GetPostListByIDs(ids)
	if err != nil {
		return
	}
	for idx, post := range posts {
		username, err := mysql.GetUserByID(post.AuthorID)
		if err != nil {
			zap.L().Error("mysql.GetUserByID failed ", zap.Error(err))
			continue
		}
		community, err := mysql.GetCommunityDetailByID(post.CommunityID)
		if err != nil {
			zap.L().Error("mysql.GetCommunityDetailByID ", zap.Error(err))
			continue
		}
		postDetail := &models.ApiPostDetail{
			Post:            post,
			VoteNum:         voteData[idx],
			AuthorName:      username,
			CommunityDetail: community,
		}
		data = append(data, postDetail)
	}

	return

}

func PostVote(userid int64, p *models.VoteData) error {
	zap.L().Debug("PostVote",
		zap.Int64("userid", userid),
		zap.String("postid", p.PostID),
		zap.Int8("direction", p.Direction))
	return redis.PostVote(strconv.Itoa(int(userid)), p.PostID, float64(p.Direction))
}

func GetCommunityPostList2(p *models.ParamPostList)(data []*models.ApiPostDetail,err error){

	ids, err := redis.GetCommunityPostIDsInOrder(p)
	if err != nil {
		return
	}
	if len(ids) == 0 {
		zap.L().Warn("redis.GetPostIDsInOrder return 0 data")
		return
	}
	voteData, err := redis.GetPostvotedData(ids)
	if err != nil {
		return
	}
	//根据id去mysql数据库查询帖子列表
	posts, err := mysql.GetPostListByIDs(ids)
	if err != nil {
		return
	}
	for idx, post := range posts {
		username, err := mysql.GetUserByID(post.AuthorID)
		if err != nil {
			zap.L().Error("mysql.GetUserByID failed ", zap.Error(err))
			continue
		}
		community, err := mysql.GetCommunityDetailByID(post.CommunityID)
		if err != nil {
			zap.L().Error("mysql.GetCommunityDetailByID ", zap.Error(err))
			continue
		}
		postDetail := &models.ApiPostDetail{
			Post:            post,
			VoteNum:         voteData[idx],
			AuthorName:      username,
			CommunityDetail: community,
		}
		data = append(data, postDetail)
	}

	return
}

func GetPostListNew(p *models.ParamPostList)(data []*models.ApiPostDetail,err error){
	if p.CommunityID==0{
		//查所有
		data, err = GetPostList2(p)
	}else {
		//根据社区id查询
		data, err = GetCommunityPostList2(p)
	}
	if err!=nil{
		zap.L().Error("GetPostListNew failed",zap.Error(err))
	}
	return
}